﻿using System;
using System.Data;

namespace ApplicationLibrary.SqlConnector
{
    #region DRCounter

    public class RRCounter : DataReaderUpdater
    {
        private int year;
        private int type;
        private SqlABParameter sqlP1;
        private SqlABParameter sqlP2;

        public RRCounter(bool updater)
            : base(updater)
        {
        }

        public bool Find(int year, int type)
        {
            this.year = year;
            this.type = type;
            return base.Find();
        }

        protected override void CreateConnection()
        {
            myTable = AM_Counter.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", AM_Counter.Year);
            sqlP2 = new SqlABParameter("@p51", AM_Counter.Type);
            qB.AddSelect();
            qB.AddFrom(AM_Counter.Name);
            qB.AddCompare(AM_Counter.Year, sqlP1, "<=");
            qB.AddCompare(AM_Counter.Type, sqlP2);

            qB.AddOrder(AM_Counter.Year, true);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlCM.Parameters.Add(sqlP2);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = year;
            sqlP2.Value = type;
        }
    }

    #endregion

    #region DUCounterValue

    public class DUCounterValue : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private SqlABParameter sqlP2;
        private int type;
        private string code;

        public DUCounterValue(bool updater)
            : base(updater)
        {
        }

        public bool Find(int type, string code)
        {
            this.type = type;
            this.code = code;
            return base.Find();
        }

        protected override void CreateConnection()
        {
            myTable = AM_CounterValue.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", AM_CounterValue.Type);
            sqlP2 = new SqlABParameter("@p51", AM_CounterValue.Code);
            qB.AddSelect();
            qB.AddFrom(AM_CounterValue.Name);
            qB.AddCompare(AM_CounterValue.Type, sqlP1);
            qB.AddCompare(AM_CounterValue.Code, sqlP2);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlCM.Parameters.Add(sqlP2);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = type;
            sqlP2.Value = code;
        }

        public override DataRow AddRecord()
        {
            DataRow dr = base.AddRecord();
            dr[AM_CounterValue.Type.Name] = sqlP1.Value;
            dr[AM_CounterValue.Code.Name] = sqlP2.Value;

            return dr;
        }
    }

    #endregion

    #region DRCodes

    public class DUCodes : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private string codetype;

        public DUCodes(bool updater)
            : base(updater)
        {
        }

        protected override void CreateConnection()
        {
            myTable = AM_CodeSegment.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", AM_CodeSegment.CodeType);
            qB.AddSelect();
            qB.AddFrom(AM_CodeSegment.Name);
            qB.AddCompare(AM_CodeSegment.CodeType, sqlP1);
            qB.AddOrder(AM_CodeSegment.Segment);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string codeType)
        {
            codetype = codeType;
            return base.Find();
        }

        protected override void SetParameters()
        {
            sqlP1.Value = codetype;
        }
    }

    #endregion

    #region DRPreference

    public class DUPreference : DataReaderUpdater
    {
        private SqlABParameter sqlP1 = null;
        private SqlABParameter sqlP2 = null;
        private SqlABParameter sqlP3 = null;
        private SqlABParameter sqlP4 = null;
        private string preftype;
        private string computer;
        private string username;
        private string application;

        public DUPreference(bool updater)
            : base(updater)
        {
        }

        public bool Find(string PrefType, string Computer, string Username, string Application)
        {
            preftype = PrefType;
            computer = Computer;
            username = Username;
            application = Application;

            return base.Find();
        }

        protected override void CreateConnection()
        {
            myTable = AM_Preferences.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", AM_Preferences.PrefType);
            sqlP2 = new SqlABParameter("@p51", AM_Preferences.Computer);
            sqlP3 = new SqlABParameter("@p52", AM_Preferences.Username);
            sqlP4 = new SqlABParameter("@p53", AM_Preferences.Application);
            qB.AddSelect();
            qB.AddFrom(AM_Preferences.Name);
            qB.AddCompare(AM_Preferences.PrefType, sqlP1);
            qB.AddCompare(AM_Preferences.Computer, sqlP2);
            qB.AddCompare(AM_Preferences.Username, sqlP3);
            qB.AddCompare(AM_Preferences.Application, sqlP4);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlCM.Parameters.Add(sqlP2);
            sqlCM.Parameters.Add(sqlP3);
            sqlCM.Parameters.Add(sqlP4);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public DataRow AddRecord(string pref)
        {
            DataRow dr = base.AddRecord();
            dr[AM_Preferences.PrefType.Name] = preftype;
            dr[AM_Preferences.Computer.Name] = computer; ;
            dr[AM_Preferences.Username.Name] = username;
            dr[AM_Preferences.Application.Name] = application;
            dr[AM_Preferences.Preferences.Name] = pref;

            return dr;
        }

        public DataRow AddRecord(string preftype, string computername, string username, string application)
        {
            DataRow dr = base.AddRecord();
            dr[AM_Preferences.PrefType.Name] = preftype;
            dr[AM_Preferences.Computer.Name] = computername;
            dr[AM_Preferences.Username.Name] = username;
            dr[AM_Preferences.Application.Name] = application;

            return dr;
        }

        protected override void SetParameters()
        {
            sqlP1.Value = preftype;
            sqlP2.Value = computer;
            sqlP3.Value = username;
            sqlP4.Value = application;
        }
    }

    #endregion

    #region DRFindPreferences

    public class DRFindPreference : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private SqlABParameter sqlP2;
        private string preftype;
        private string application;

        public DRFindPreference(bool updater)
            : base(updater)
        {
        }

        public bool Find(string preftype, string application)
        {
            this.preftype = preftype;
            this.application = application;

            return base.Find();
        }

        protected override void CreateConnection()
        {
            myTable = AM_Preferences.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", AM_Preferences.PrefType);
            sqlP2 = new SqlABParameter("@p51", AM_Preferences.Application);
            qB.AddSelect();
            qB.AddFrom(AM_Preferences.Name);
            qB.AddCompare(AM_Preferences.PrefType, sqlP1);
            qB.AddCompare(AM_Preferences.Application, sqlP2);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlCM.Parameters.Add(sqlP2);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = preftype;
            sqlP2.Value = application;
        }
    }

    #endregion

    #region DRReadAllPreference

    public class RRReadAllPreference : DataReaderUpdater
    {
        private SqlABParameter sqlP1, sqlP2, sqlP3, sqlP4;
        private string preftype;
        private string computer;
        private string username;
        private string application;

        public RRReadAllPreference(bool updater)
            : base(updater)
        {
        }

        public bool Find(string preftype, string computer, string username, string application)
        {
            this.preftype = preftype;
            this.computer = computer;
            this.username = username;
            this.application = application;

            return base.Find();
        }

        protected override void CreateConnection()
        {
            myTable = AM_Preferences.Name;

            QueryBuilder qB = new QueryBuilder();
            QueryBuilder qB2 = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", AM_Preferences.PrefType);
            sqlP2 = new SqlABParameter("@p51", AM_Preferences.Computer);
            sqlP3 = new SqlABParameter("@p52", AM_Preferences.Username);
            sqlP4 = new SqlABParameter("@p53", AM_Preferences.Application);

            qB.AddSelect();
            qB.AddFrom(AM_Preferences.Name);
            qB.AddCompare(AM_Preferences.PrefType, sqlP1);
            string compOr = "( " + qB2.AddCompare(AM_Preferences.Computer, sqlP2) + " OR " + qB2.AddCompare(AM_Preferences.Computer, "''") + " )";
            qB.Where = qB.AddString(qB.Where, compOr, "AND");
            compOr = "( " + qB2.AddCompare(AM_Preferences.Username, sqlP3) + " OR " + qB2.AddCompare(AM_Preferences.Username, "''") + " )";
            qB.Where = qB.AddString(qB.Where, compOr, "AND");
            compOr = "( " + qB2.AddCompare(AM_Preferences.Application, sqlP4) + " OR " + qB2.AddCompare(AM_Preferences.Application, "''") + " )";
            qB.Where = qB.AddString(qB.Where, compOr, "AND");

            qB.AddOrder(AM_Preferences.Computer, true);
            qB.AddOrder(AM_Preferences.Username, true);
            qB.AddOrder(AM_Preferences.Application, true);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlCM.Parameters.Add(sqlP2);
            sqlCM.Parameters.Add(sqlP3);
            sqlCM.Parameters.Add(sqlP4);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = preftype;
            sqlP2.Value = computer;
            sqlP3.Value = username;
            sqlP4.Value = application;
        }
    }

    #endregion

    #region DULocks

    public class DULocks : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private SqlABParameter sqlP2;
        private string tablename;
        private string key;

        public DULocks(bool updater)
            : base(updater)
        {
        }

        public bool Find(string tablename, string key)
        {
            this.tablename = tablename;
            this.key = key;
            return base.Find();
        }

        protected override void CreateConnection()
        {
            myTable = AM_Locks.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", AM_Locks.TableName);
            sqlP2 = new SqlABParameter("@p51", AM_Locks.LockKey);

            qB.AddSelect();
            qB.AddFrom(AM_Locks.Name);
            qB.AddCompare(AM_Locks.TableName, sqlP1);
            qB.AddCompare(AM_Locks.LockKey, sqlP2);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlCM.Parameters.Add(sqlP2);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = tablename;
            sqlP2.Value = key;
        }

        public DataRow AddRecord(string computername, string username, DateTime lockdate)
        {
            DataRow dr = base.AddRecord();
            dr[AM_Locks.TableName.Name] = sqlP1.Value;
            dr[AM_Locks.LockKey.Name] = sqlP2.Value;

            dr[AM_Locks.ComputerName.Name] = computername;
            dr[AM_Locks.Username.Name] = username;
            dr[AM_Locks.LockDate.Name] = lockdate;

            return dr;
        }
    }

    #endregion
}